package com.hxy.manager.activity;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hxy.manager.R;
import com.hxy.manager.entity.GoodsEntity;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by 傲寒宏志 on 2018/1/13.
 */

public class RecordActivity extends Activity {
   // EditText editText0;
    EditText editText1;
    EditText editText2;
    EditText editText3;
    EditText editText4;
    EditText editText5;
    EditText editText6;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.record);
        setTitle("登记商品");

        getActionBar().setDisplayHomeAsUpEnabled(true);//向上返回
     //   editText0=(EditText)findViewById(R.id.editText0);
        editText1=(EditText)findViewById(R.id.editText1);
        editText2=(EditText)findViewById(R.id.editText2);
        editText3=(EditText)findViewById(R.id.editText3);
        editText4=(EditText)findViewById(R.id.editText4);
        editText5=(EditText)findViewById(R.id.editText5);
        editText6=(EditText)findViewById(R.id.editDateEditText);
        Calendar now = Calendar.getInstance();
        editText6.setText(now.get(Calendar.YEAR)+"年"+(now.get(Calendar.MONTH) + 1) +"月"+now.get(Calendar.DAY_OF_MONTH)+"日");





        Button save =(Button)findViewById(R.id.save);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //取当前年月日时分秒最为编码
                Date d = new Date();
                //格式化为一串数字
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                String dateNowStr = sdf.format(d);


                if(editText1.getText().toString().equals("")){
                    //  if(editText0.getText().toString().equals("")||editText1.getText().toString().equals("")||editText2.getText().toString().equals("")||editText3.getText().toString().equals("")||editText4.getText().toString().equals("")||editText4.getText().toString().equals("")||editText5.getText().toString().equals("")||editText6.getText().toString().equals("")) {

                    Toast.makeText(RecordActivity.this,"名称不可为空！",Toast.LENGTH_SHORT).show();

                }else
                if(editText2.getText().toString().equals("")){
                    //  if(editText0.getText().toString().equals("")||editText1.getText().toString().equals("")||editText2.getText().toString().equals("")||editText3.getText().toString().equals("")||editText4.getText().toString().equals("")||editText4.getText().toString().equals("")||editText5.getText().toString().equals("")||editText6.getText().toString().equals("")) {

                    Toast.makeText(RecordActivity.this,"价格不可为空！",Toast.LENGTH_SHORT).show();

                }else
                {

                    GoodsEntity user = new GoodsEntity();
                    //服务器接收数据，并且写入到数据库
                    //  user.setPsw("receive");
                    user.setNumber(dateNowStr);
                    user.setName(editText1.getText().toString());
                    user.setPrice(editText2.getText().toString());
                    user.setPress(editText3.getText().toString());
                    user.setDate(editText4.getText().toString());
                    user.setAuthor(editText5.getText().toString());
                    user.setLocation(editText6.getText().toString());
                    user.setUpdateTime(System.currentTimeMillis());
//                    MakeTable mt=new MakeTable(Record.this);
//
//                    if(mt.addData(user)){
//                        Toast.makeText(Record.this,"保存成功！",Toast.LENGTH_SHORT).show();
//               //         editText0.setText("");
//                        editText1.setText("");
//                        editText2.setText("");
//                        editText3.setText("");
//                        editText4.setText("");
//                        editText5.setText("");
//                     //   editText6.setText("");
//                    }
                }

            }
        });


    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
      //  getMenuInflater().inflate(R.menu.main,menu);
        return super.onCreateOptionsMenu(menu);
    }


    public boolean onOptionsItemSelected(MenuItem item){

        switch(item.getItemId()){
        case android.R.id.home:  //默认添加的
		      finish();  
		      break;  }
        return false;
    }
}
